from requests import Session
from bs4 import BeautifulSoup
import downloader
from pathlib import Path
import json
import utils
import translator

session = Session()
session.trust_env = False
session.adapters.DEFAULT_RETRIES=10
session.headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}

effect_icon_path = Path("img/status-effects")
effect_icons = {file.name for file in effect_icon_path.glob("*")}

def get_status_effect(table:BeautifulSoup)->dict:
    effect = {}
    rows = table.select("tr")
    effect["name"]=rows[0].get_text(strip=True) # 第一行是buff的名字
    effect["nameEn"] = effect["name"]
    # 第2行是buff的图标以及buff的描述
    tds = rows[1].select("td")
    imgs = tds[0].select("img") # 图标一般有两张
    # 第1张是背景图，buff为"Positive_Buff.png",生物buff为"Neutral_Buff.png",debuff为"Negative_Buff.png"
    img_name = imgs[0].get("data-image-name")
    if img_name == "Positive Buff.png":
        effect["type"] = "positive"
    elif img_name == "Neutral Buff.png":
        effect["type"] = "neutral"
    elif img_name == "Negative Buff.png":
        effect["type"] = "negative"
    else:
        effect["type"] = "other"
    # 如果背景图不存在的话，则加入到下载队列
    if img_name not in effect_icons:
        img_src = imgs[0].get("data-src")
        if img_src is None:
            img_src = imgs[0].get("src")
        img_src=img_src.split("/revision/",1)[0]
        downloader.add_task(img_src, effect_icon_path/img_name)
    # 获取buff的图标
    img_name = imgs[-1].get("data-image-name")
    effect["img"] = img_name
    # 如果buff的图标不存在的话，则加入下载队列
    if img_name not in effect_icons:
        img_src = imgs[-1].get("data-src").split("/revision/",1)[0]
        downloader.add_task(img_src, effect_icon_path/img_name)
    # 获取buff的描述
    for p in tds[1].select("p"):
        text = p.text.strip()
        if text == "":
            continue
        if "Message: " in text:
            effect["message"] = text[9:]
        elif "HUD Text: " in text:
            effect["hudText"]=text[10:]
        elif "Text: " in text:
            effect["hudText"] = text[6:]
        elif "Information: " in text:
            effect["infomation"] = text[13:]
        elif "Note:" in text or "Notes:" in text:
            lis=p.find_next_sibling("ul").select("li")
            effect["notes"] = [li.text.strip() for li in lis]
        else:
            if "infomation" in effect:
                effect["infomation"] +=text
            else:
                effect["infomation"] = text
    return effect

def scrap_all_status_effects(translate=True):
    """ 获取所有状态效果 """
    print("正在请求列表页")
    resp = session.get("https://ark.fandom.com/wiki/Status_effects")
    soup = BeautifulSoup(resp.content,"lxml")
    tables = soup.select(".status-effect-backg")
    status_effects = [get_status_effect(table) for table in tables]
    # 翻译
    if translate:
        translator.translate_status_effects(status_effects,update_translations=True)
    # 保存数据
    utils.save_data(status_effects, "data/status-effects.json")
